package com.cow.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.cow.pojo.Good;
import com.cow.pojo.ShoppingCart;
import com.cow.service.GoodService;
import com.cow.service.impl.ShoppingCartServiceImpl;
import com.cow.util.FileUtil;
import com.cow.util.GoodUtilBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author 光环and鬼泣
 * @version 2018年5月30日 上午10:55:39
 */
@Api(description = "商品接口")
@Controller
public class GoodController {

	@Autowired
	private GoodService goodService;
	@Autowired
	private ShoppingCartServiceImpl shoppingCartServiceImpl;

	@ApiOperation(value = "获取商品列表", notes = "分页的商品列表")
	@ResponseBody
	@RequestMapping(value = "/getGoodList.action", method = RequestMethod.POST)
	public JSONObject getGoodList(HttpServletRequest req, HttpServletResponse rep, @RequestParam("page") String page)
			throws Exception {

		List<Good> goods = goodService.getGoodList();
		List<GoodUtilBean> currentGoods = new ArrayList<GoodUtilBean>();
		int totalPage = goods.size() / 10 + 1;
		for (int i = 0; i < goods.size(); i++) {
			if (i >= (Integer.parseInt(page) - 1) * 10 && i < (Integer.parseInt(page) * 10)) {
				currentGoods.add(new GoodUtilBean(goods.get(i)));
			}
		}
		JSONObject data = new JSONObject();
		data.put("totalPage", totalPage);
		data.put("page", page);
		data.put("goodList", JSONArray.fromObject(currentGoods));
		return data;
	}

	@ApiOperation(value = "获取全部商品列表", notes = "未分页的商品列表")
	@ResponseBody
	@RequestMapping(value = "/getAllGoodList.action", method = RequestMethod.POST)
	public JSONObject getAllGoodList(HttpServletRequest req, HttpServletResponse rep) throws Exception {
		List<Good> goods = goodService.getGoodList();
		// List<GoodUtilBean> currentGoods = new ArrayList<GoodUtilBean>();
		// System.out.println(page);
		// int totalPage = goods.size() / 5 + 1;
		// for (int i = 0; i < goods.size(); i++) {
		// if (i >= (Integer.parseInt(page) - 1) * 5 && i < (Integer.parseInt(page) *
		// 5)) {
		// currentGoods.add(new GoodUtilBean(goods.get(i)));
		// }
		// }
		JSONObject data = new JSONObject();
		// data.put("totalPage", totalPage);
		// data.put("page", page);
		data.put("goodList", JSONArray.fromObject(goods));
		return data;

	}

	@ApiOperation(value = "搜索商品信息", notes = "搜索商品信息")
	@ResponseBody
	@RequestMapping(value = "/searchGood.action", method = RequestMethod.POST)
	public JSONObject searchGood(HttpServletRequest req, HttpServletResponse rep, @RequestParam("value") String value)
			throws Exception {
		List<Good> goods = goodService.searchGood(value);
		List<GoodUtilBean> currentGoods = new ArrayList<GoodUtilBean>();
		for (int i = 0; i < goods.size(); i++) {
			currentGoods.add(new GoodUtilBean(goods.get(i)));
		}
		JSONObject data = new JSONObject();
		data.put("goodList", currentGoods);
		return data;

	}

	@ApiOperation(value = "新增商品", notes = "新增商品")
	@ResponseBody
	@RequestMapping(value = "/insertGood.action", method = RequestMethod.POST)
	public JSONObject insertGood(HttpServletRequest req, HttpServletResponse rep,
			@RequestParam("file") MultipartFile file, @RequestParam("goodName") String goodName,
			@RequestParam("goodPrice") String goodPrice, @RequestParam("goodDescribe") String goodDescribe,
			@RequestParam("goodDetail") String goodDetail) throws Exception {
		Good good = new Good();
		good.setGoodDescribe(goodDescribe);
		good.setGoodDetail(goodDetail);
		good.setGoodId(String.valueOf(UUID.randomUUID()));
		good.setGoodName(goodName);
		good.setGoodPrice(goodPrice);
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String addTime = sdf.format(date);
		good.setGoodAddtime(addTime);
		String path = req.getServletContext().getRealPath("upload");
		String filePath = FileUtil.fileUpload(file, path);
		good.setGoodPicture(filePath);
		int result = goodService.insertGood(good);
		JSONObject datas = new JSONObject();
		if (result == 1) {
			datas.put("result", "商品上传成功");
			datas.put("code", "1");
		} else {
			datas.put("result", "商品上传失败");
			datas.put("code", "0");
		}
		return datas;

	}

	@ApiOperation(value = "删除商品", notes = "删除商品")
	@ResponseBody
	@RequestMapping(value = "/deleteGood.action", method = RequestMethod.POST)
	public JSONObject deleteGood(HttpServletRequest req, HttpServletResponse rep, @RequestParam("goodId") String goodId)
			throws Exception {
		int result = goodService.deleteGood(goodId);
		JSONObject datas = new JSONObject();
		if (result == 1) {
			datas.put("result", "商品删除成功");
			datas.put("code", "1");
		} else {
			datas.put("result", "商品删除失败");
			datas.put("code", "0");
		}
		return datas;

	}

	@ApiOperation(value = "获取商品详情信息", notes = "获取商品详情信息")
	@ResponseBody
	@RequestMapping(value = "/getGoodDetail.action", method = RequestMethod.GET)
	public JSONObject getGoodDetail(HttpServletRequest req, HttpServletResponse rep,
			@RequestParam("goodId") String goodId) throws Exception {
		Good good = goodService.getGoodDetail(goodId);
		JSONObject data = new JSONObject();
		data.put("goodDetail", good);
		return data;
	}

	@ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车")
	@ResponseBody
	@RequestMapping(value = "/addshoppingCart.action", method = RequestMethod.GET)
	public JSONObject addshoppingCart(HttpServletRequest req, HttpServletResponse rep,
			@RequestParam("goodId") String goodId, @RequestParam("userId") String userId,
			@RequestParam("goodNumber") String goodNumber) throws Exception {
		ShoppingCart shoppingCart = new ShoppingCart();
		shoppingCart.setGoodId(goodId);
		shoppingCart.setGoodNumber(goodNumber);
		shoppingCart.setUserId(userId);
		int result = shoppingCartServiceImpl.addGoodToShoppingCart(shoppingCart);
		JSONObject datas = new JSONObject();
		if (result == 1) {
			datas.put("result", "添加购物车成功");
			datas.put("code", "1");
		} else {
			datas.put("result", "添加购物车失败");
			datas.put("code", "0");
		}
		return datas;

	}

	@ApiOperation(value = "从购物车中删除商品", notes = "从购物车中删除商品")
	@ResponseBody
	@RequestMapping(value = "/deleteshoppingCart.action", method = RequestMethod.POST)
	public JSONObject deleteshoppingCart(HttpServletRequest req, HttpServletResponse rep,@RequestParam("userId") String userId,
			@RequestParam("goodList") String goodList) throws Exception {
		JSONArray goods = JSONArray.fromObject(goodList);
		int result = 0;
		for (int i = 0; i < goods.size(); i++) {
			result = result
					+ shoppingCartServiceImpl.deleteGoodToShoppingCart(String.valueOf(goods.get(i)), userId);
		}
		JSONObject datas = new JSONObject();
		if (result == goods.size()) {
			datas.put("result", "购物车商品删除成功");
			datas.put("code", "1");
		} else {
			datas.put("result", "购物车商品删除失败");
			datas.put("code", "0");
		}
		return datas;
	}

	@ApiOperation(value = "获取用户购物车商品列表", notes = "获取用户购物车商品列表")
	@ResponseBody
	@RequestMapping(value = "/getshoppingCart.action", method = RequestMethod.POST)
	public JSONObject getshoppingCart(HttpServletRequest req, HttpServletResponse rep,
			@RequestParam("userId") String userId) throws Exception {
		List<GoodUtilBean> goodList = shoppingCartServiceImpl.getShoppingCart(userId);
		JSONObject data = new JSONObject();
		data.put("goodList", goodList);
		return data;
	}
}
